package com.taiji.dianthus.service.http.impl;

import com.alibaba.fastjson.JSONArray;
import com.taiji.dianthus.common.BusinessMessage;
import com.taiji.dianthus.dao.sys.OrgRepository;
import com.taiji.dianthus.domain.sys.DSysOrg;
import com.taiji.dianthus.service.http.HttpAPIService;
import com.taiji.dianthus.service.http.SysOrgInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName SysOrgInfoServiceImpl
 * @Description
 * @Author H.M
 * @Date 2020/5/27
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysOrgInfoServiceImpl implements SysOrgInfoService {

    @Autowired
    private OrgRepository orgRepository;

    @Autowired
    private HttpAPIService httpAPIService;


    /**
     * 同步组织信息
     *
     * @param url
     * @return
     */
    @Override
    public BusinessMessage SynAllOrg(String url) {
        List<DSysOrg> orgList =  orgRepository.listAll();
        JSONArray userJsonArray = new JSONArray(new ArrayList<>(orgList));
        return  httpAPIService.doPost(url,userJsonArray);
    }

    /**
     * 删除单个组织
     *
     * @param url
     * @param orgId
     */
    @Override
    public BusinessMessage removeOrg(String url, String orgId) {
        Map orgMap = new HashMap(16);
        orgMap.put("id",orgId);
        return httpAPIService.doGet(url,orgMap);
    }

    /**
     * 更新组织信息
     *
     * @param url
     * @param orgId
     */
    @Override
    public BusinessMessage updateOrg(String url, String orgId) {
        DSysOrg org = orgRepository.getById(orgId);
        BusinessMessage message = new BusinessMessage();
        Map orgMap = new HashMap(16);
        if (null == org) {
            orgMap.put("id",org.getId());
            orgMap.put("orgName",org.getOrgName());
            orgMap.put("parentId",org.getParentId());
            orgMap.put("orgLevel",org.getOrgLevel());
            orgMap.put("orgOrder",org.getOrgOrder());
            httpAPIService.doPost(url, orgMap);
        }else {
            message.setMsg("用户不存在");
        }

        return message;
    }
}
